package com.example.goup17booksmanagement;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class AddAndEdit extends Activity {
	private Context mContext;
	private Button btnSave, btnCancel, btnPickDate;
	private EditText edtName, edtAuthor, edtPublisher, edtPublishDate;
	private ImageView img;
	String index = "";
	String id = "";
	public static final int DATE_DIALOG_ID = 111;
	private int year = -1, month = -1, day = -1;
	String editDay = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_and_edit);
		
		// Get current Calendar for DatePicker
		Calendar temp = Calendar.getInstance();
		year = temp.get(Calendar.YEAR);
		month = temp.get(Calendar.MONTH);
		day = temp.get(Calendar.DAY_OF_MONTH);

		mContext = this;

		// UI Binding
		btnSave = (Button) findViewById(R.id.btnSave);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		edtName = (EditText) findViewById(R.id.edtName);
		edtAuthor = (EditText) findViewById(R.id.edtAuthor);
		edtPublisher = (EditText) findViewById(R.id.edtPublisher);
		edtPublishDate = (EditText) findViewById(R.id.edtPublishDate);
		img = (ImageView) findViewById(R.id.imgAvatar);
		btnPickDate = (Button) findViewById(R.id.btnPickDate);

		// Receiver Data
		Intent receiver = getIntent();

		String mode = receiver.getStringExtra("mode");
		if (mode.equals("edit_mode")) {
			// Install ViewMode or Edit Mode
			img.setImageResource(R.drawable.book1);
			
			id = receiver.getStringExtra("id");
			String name = receiver.getStringExtra("name");
			String author = receiver.getStringExtra("author");
			String publisher = receiver.getStringExtra("publisher");
			String publishDate = receiver.getStringExtra("publishdate");
			index = receiver.getStringExtra("index");

			edtName.setText(name);
			edtAuthor.setText(author);
			edtPublisher.setText(publisher);
			edtPublishDate.setText(publishDate);
		}
		if (mode.equals("create_new_mode")) {
			installCreateNewMode();
		}

		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		btnSave.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Save all Data and ...

				if (edtName.getText().toString().equals("") == false) {
					Intent senderIntent = getIntent();
					senderIntent.putExtra("name", edtName.getText().toString());
					senderIntent.putExtra("index", index);
					senderIntent.putExtra("id", id);
					senderIntent.putExtra("author", edtAuthor.getText()
							.toString());
					senderIntent.putExtra("publisher", edtPublisher.getText()
							.toString());
					senderIntent.putExtra("publishdate", edtPublishDate
							.getText().toString());
					setResult(RESULT_OK, senderIntent);
					
					// Creat new Object and Insert Into Database and ListView
					finish();
				} else {
					Toast.makeText(mContext, "Book name must not empty", Toast.LENGTH_SHORT).show();
				}

				
			}
		});
		
		btnPickDate.setOnClickListener(new OnClickListener() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDialog(DATE_DIALOG_ID);
			}
		});
		
	}

	private void installCreateNewMode() {
		img.setImageResource(R.drawable.book_add);
		edtName.setText("");
		edtAuthor.setText("");
		edtPublisher.setText("");
		edtPublishDate.setText("");
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.add_and_edit, menu);
		return true;
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			// set date picker as current date
			return new DatePickerDialog(this, datePickerListener, year, month,
					day);
		}
		return null;
	}

	private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

		// when dialog box is closed, below method will be called.
		public void onDateSet(DatePicker view, int selectedYear,
				int selectedMonth, int selectedDay) {
			year = selectedYear;
			month = selectedMonth;
			day = selectedDay;

			editDay = "" + day + "/" + month + "/" + year;
			// set selected date into datepicker also
			edtPublishDate.setText(editDay);

		}
	};

}
